//   App Tittle: Free Note
//	Institution: Arizona State University
//	 Developers: Rodrigo Rodriguez, Andy Nguyen, Candise Orr
//	       Date: 3/27/2011

#import "Utilities.h"


@implementation Utilities

// simple class function to look up the user's documents directory within the app
+(NSString*)documentDirectoryPath
{
	NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
	return [documentDirectoryPath stringByAppendingPathComponent : @"Notes.plist" ];
}

+(void) writeNoteToFile: (NSMutableArray *)notes
{
	NSString *filepath = [Utilities documentDirectoryPath];
	NSData *dataToWrite = [[NSData alloc] init];
	dataToWrite = 
	[NSPropertyListSerialization dataWithPropertyList:notes
											   format:NSPropertyListXMLFormat_v1_0
											  options:0
												error:nil];
	
	if (dataToWrite != nil)
		[dataToWrite writeToFile:filepath atomically:NO];
}

+(NSMutableArray *) readNoteFromFile
{
	NSString *filepath = [Utilities documentDirectoryPath];
	NSMutableArray * notes = [[NSMutableArray alloc] init];
	
	//Retrieving data from File
	NSData *arrayData = [[NSData alloc] init];
	arrayData = [NSData dataWithContentsOfFile:filepath];
	notes = [NSPropertyListSerialization propertyListWithData:arrayData
												  options:NSPropertyListMutableContainersAndLeaves
												   format:nil
													error:nil];
	
	return notes;
	
}
	@end

